const express = require('express');
const router = express.Router();
const Product = require('../../models/product');
const { generateId } = require('../../utils/auth');

// 获取产品列表
router.get('/list', async (req, res) => {
  try {
    const { page = 1, pageSize = 10, name, category, brand, status } = req.query;
    const query = {};

    if (name) {
      query.name = new RegExp(name, 'i');
    }
    if (category) {
      query.category = category;
    }
    if (brand) {
      query.brand = brand;
    }
    if (status !== undefined) {
      query.status = status === 'true';
    }

    const total = await Product.countDocuments(query);
    const products = await Product.find(query)
      .populate('category')
      .populate('brand')
      .skip((page - 1) * pageSize)
      .limit(Number(pageSize))
      .sort({ createdAt: -1 });

    res.json({
      success: true,
      data: {
        list: products,
        total,
        page: Number(page),
        pageSize: Number(pageSize)
      }
    });
  } catch (error) {
    res.status(500).json({
      code:500,
      message: error.message
    });
  }
});

// 创建产品
router.post('/create', async (req, res) => {
  try {
    const productData = {
      ...req.body,
      id: generateId('prod_')
    };

    const product = new Product(productData);
    await product.save();

    res.json({
      success: true,
      data: product
    });
  } catch (error) {
    res.status(500).json({
      code:500,
      message: error.message
    });
  }
});

// 更新产品
router.post('/update', async (req, res) => {
  try {
    const { id, ...updateData } = req.body;

    const product = await Product.findOneAndUpdate(
      { id },
      updateData,
      { new: true }
    )
    .populate('category')
    .populate('brand');

    if (!product) {
      return res.status(404).json({
        code:500,
        message: '产品不存在'
      });
    }

    res.json({
      success: true,
      data: product
    });
  } catch (error) {
    res.status(500).json({
      code:500,
      message: error.message
    });
  }
});

// 删除产品
router.post('/delete', async (req, res) => {
  try {
    const { id } = req.body;

    const product = await Product.findOneAndDelete({ id });
    if (!product) {
      return res.status(404).json({
        code:500,
        message: '产品不存在'
      });
    }

    res.json({
      success: true,
      message: '删除成功'
    });
  } catch (error) {
    res.status(500).json({
      code:500,
      message: error.message
    });
  }
});

// 获取产品详情
router.get('/:id', async (req, res) => {
  try {
    const product = await Product.findOne({ id: req.params.id })
      .populate('category')
      .populate('brand');

    if (!product) {
      return res.status(404).json({
        code:500,
        message: '产品不存在'
      });
    }

    res.json({
      success: true,
      data: product
    });
  } catch (error) {
    res.status(500).json({
      code:500,
      message: error.message
    });
  }
});

module.exports = router; 